﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;
using devtm.Helpers;
using System.ComponentModel;
using devtm.Collections;
using System.Threading;


namespace devtm.Editor.Configuration.Customs
{

    public class CustomTemplate : INotifyPropertyChanged
    {

        public CustomTemplate() 
        {

        }


        #region Generate New Item


        public void Create(Type type)
        {


            #region Labels

            string[] paths = Path.Split('\\');
            string name = paths[paths.Length - 1];

            Labels.Add(new CustomLabel()
            {
                Culture = Thread.CurrentThread.CurrentCulture.ThreeLetterWindowsLanguageName,
                Text = name
            });

            #endregion

            #region Properties

            foreach (PropertyInfo prop in type.GetProperties())
            {

                if (prop.PropertyType.IsOfType(
                   typeof(ConfigurationElement),
                   typeof(System.Configuration.ElementInformation),
                   typeof(System.Configuration.Configuration),
                   typeof(System.Configuration.ConfigurationLockCollection)
                   )
                   || "|SectionInformation|LockItem|IsSynchronized|SyncRoot|EmitClear|Count|AllKeys|".Contains("|" + prop.Name + "|"))
                    continue;

                CustomProperty p;

                if (Properties.ContainsKey(prop.Name))
                    p = Properties[prop.Name];
                else
                {
                    p = new CustomProperty()
                    {
                        Name = prop.Name,
                        PropertyType = prop.PropertyType.FullName,
                        LastUpdate = DateTime.Now
                    };

                    p.PropertyChanged += c_PropertyChanged;

                    p.Create(prop);
                    Properties.Add(p);
                }

            }

            #endregion


        }

        #endregion

        #region Properties

        public string Path { get; set; }


        private DateTime _LastUpdate;
        [System.Xml.Serialization.XmlAttribute()]
        public DateTime LastUpdate
        {
            get
            {
                return _LastUpdate;
            }
            set
            {
                if (_LastUpdate == value)
                    return;
                _LastUpdate = value;

                PropertyChange("LastUpdate");
            }
        }


        public IndexedObservableCollection<CustomProperty, string>
            Properties = new IndexedObservableCollection<CustomProperty, string>
                (c => c.Name);

        public IndexedObservableCollection<CustomLabel, string>
            Labels = new IndexedObservableCollection<CustomLabel, string>
                (c => c.Culture);


        #endregion

        #region Change

        private void PropertyChange(string p)
        {

            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));

            if (p != "LastUpdate")
                LastUpdate = DateTime.Now;

        }

        void c_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs f)
        {

            if (f.PropertyName == "LastUpdate")
                LastUpdate = (sender as CustomProperty).LastUpdate;

        }

       
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
